package day;

import java.util.Arrays;
import java.util.List;

public class P524 {
    public String findLongestWord(String s, List<String> dictionary) {
        String result = "";
        for (String word : dictionary) {
            if (contain(s, word)) {
                if (word.length() > result.length()) {
                    result = word;
                } else if (word.length() == result.length()) {
                    result = result.compareTo(word) > 0 ? word : result;
                }
            }
        }
        return result;
    }

    private boolean contain(String s, String word) {
        int p1 = 0, p2 = 0;
        while (p1 < s.length() && p2 < word.length()) {
            if (s.charAt(p1) == word.charAt(p2)) {
                p1++;
                p2++;
            } else {
                p1++;
            }
        }
        return p2 == word.length() ? true : false;
    }

    public static void main(String[] args) {
        String result = new P524().findLongestWord("aaa", Arrays.asList("aaa", "aa", "a"));
        System.out.println(result);
    }
}
